name: CI

on:
  push:
    branches:
      - main

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v4
    - uses: actions/setup-java@v4
      with:
        distribution: "temurin"
        java-version: "17"

    - run: cd p && ./gradlew assembleDebug

    - uses: actions/upload-artifact@v4
      with:
        name: app-debug.apk
        path: p/app/build/outputs/apk/debug/app-debug.apk

  # 组装 apk 安装所需的一堆文件, 方便安装
  setup-apk:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v4

    # ui-vue (pmim-ibus)
    - uses: robinraju/release-downloader@v1
      with:
        repository: "fm-elpac/pmim-ibus"
        tag: "v0.1.7"
        fileName: "ui-vue-dist.tar.zst"
    # plugin (pmim-ibus)
    - uses: robinraju/release-downloader@v1
      with:
        repository: "fm-elpac/pmim-ibus"
        tag: "v0.1.7"
        fileName: "pmim-plugin.tar.zst"
    # pmim-server
    - uses: robinraju/release-downloader@v1
      with:
        repository: "fm-elpac/pmim"
        tag: "v0.1.4"
        fileName: "pmim-server.tar.zst"
    # pmim-data
    - uses: robinraju/release-downloader@v1
      with:
        repository: "fm-elpac/pmim-data"
        tag: "v0.1.1"
        fileName: "pmim_sys.db.zst"

    # 构建目录
    - run: mkdir build1 && mkdir files
    # 解压文件
    - run: mv ui-vue-dist.tar.zst build1 && cd build1 && tar -xvf ui-vue-dist.tar.zst
    - run: mv pmim-plugin.tar.zst build1 && cd build1 && tar -xvf pmim-plugin.tar.zst
    - run: mv pmim-server.tar.zst build1 && cd build1 && tar -xvf pmim-server.tar.zst
    - run: mv pmim_sys.db.zst build1 && cd build1 && zstd -d pmim_sys.db.zst
    # DEBUG
    - run: find build1

    # 开始组装
    - run: mv build1/server files/
    - run: mkdir -p files/server/plugin && cp -r build1/plugin/* files/server/plugin
    - run: cp -r build1/ui-vue/dist/* files/server/static/
    - run: mv build1/ui-vue/dist files/ui

    # DEBUG
    - run: find files
    # 不带数据库
    - uses: actions/upload-artifact@v4
      with:
        name: setup-pmim-apk
        path: files

    # 带数据库
    - run: mkdir files/pmim
    - run: mv build1/pmim_sys.db files/pmim/
    # DEBUG
    - run: find files
    - uses: actions/upload-artifact@v4
      with:
        name: setup-pmim-apk-db
        path: files

  # 自动构建 deno 的 proot 运行环境
  setup-proot:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v4

    # deno
    - uses: robinraju/release-downloader@v1
      with:
        repository: "denoland/deno"
        tag: "v1.41.2"
        fileName: "deno-aarch64-unknown-linux-gnu.zip"
    # proot-distro: debian12_aarch64 sysroot
    - uses: robinraju/release-downloader@v1
      with:
        repository: "termux/proot-distro"
        tag: "v4.7.0"
        fileName: "debian-bookworm-aarch64-pd-v4.7.0.tar.xz"
    # TODO termux ?

    # 构建目录
    - run: mkdir build1
    # 解压文件
    - run: mv deno-aarch64-unknown-linux-gnu.zip build1 && cd build1 && unzip deno-aarch64-unknown-linux-gnu.zip
    # 忽略错误
    - run: mv debian-bookworm-aarch64-pd-v4.7.0.tar.xz build1 && cd build1 && tar -xvf debian-bookworm-aarch64-pd-v4.7.0.tar.xz || true
    # DEBUG
    - run: find build1

    # 开始组装
    - run: mkdir -p setup-proot/setup/debian12_aarch64/usr/lib/aarch64-linux-gnu/
    - run: mkdir -p setup-proot/setup/debian12_aarch64/usr/bin/
    - run: cp build1/debian-bookworm-aarch64/usr/lib/aarch64-linux-gnu/libdl.so.2 setup-proot/setup/debian12_aarch64/usr/lib/aarch64-linux-gnu/
    - run: cp build1/debian-bookworm-aarch64/usr/lib/aarch64-linux-gnu/libm.so.6 setup-proot/setup/debian12_aarch64/usr/lib/aarch64-linux-gnu/
    - run: cp build1/debian-bookworm-aarch64/usr/lib/aarch64-linux-gnu/libgcc_s.so.1 setup-proot/setup/debian12_aarch64/usr/lib/aarch64-linux-gnu/
    - run: cp build1/debian-bookworm-aarch64/usr/lib/aarch64-linux-gnu/ld-linux-aarch64.so.1 setup-proot/setup/debian12_aarch64/usr/lib/aarch64-linux-gnu/
    - run: cp build1/debian-bookworm-aarch64/usr/lib/aarch64-linux-gnu/libpthread.so.0 setup-proot/setup/debian12_aarch64/usr/lib/aarch64-linux-gnu/
    - run: cp build1/debian-bookworm-aarch64/usr/lib/aarch64-linux-gnu/libc.so.6 setup-proot/setup/debian12_aarch64/usr/lib/aarch64-linux-gnu/
    - run: cp build1/debian-bookworm-aarch64/usr/bin/env setup-proot/setup/debian12_aarch64/usr/bin/
    - run: mv build1/deno setup-proot/setup/debian12_aarch64/usr/bin/

    # DEBUG
    - run: find setup-proot/setup
    - uses: actions/upload-artifact@v4
      with:
        name: setup-proot-deno
        path: setup-proot/setup
